package com.qf.controller;


import com.qf.constant.AppConstant;
import com.qf.entity.AppCategory;
import com.qf.entity.AppInfo;
import com.qf.entity.AppVersion;
import com.qf.enums.AppEnum;
import com.qf.enums.AppTypeEnum;
import com.qf.from.AddAppInfoFrom;
import com.qf.from.AppMaintainFrom;
import com.qf.from.AddAppVersionFrom;
import com.qf.service.AppCategoryService;
import com.qf.service.AppInfoService;
import com.qf.service.AppVersionService;
import com.qf.service.DataDictionaryService;
import com.qf.util.ResultVOUtil;
import com.qf.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 *
 * 商品模块
 *
 */

@Controller
@RequestMapping("/dev/app")
@Slf4j
public class DevAppController {

    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private AppVersionService appVersionService;

    //跳转主页面
    @GetMapping("/index")
    public String index(){
        return "dev/app/index";
    }

    //查看下载量前五
    @GetMapping("/Order-by-downloads")
    @ResponseBody
    public ResultVO orderByDownloads(){

        List<DownloadsVO> downloadsVOList = appInfoService.findOrderByDownloads();

        return ResultVOUtil.ok(downloadsVOList);
    }

    //跳转APP维护页面
    @GetMapping("/app-maintain")
    public String appMaintain(Model model){

        //查询全部app状态
        List<DataDictionaryVO> dictionaryVOList = dataDictionaryService.findValueByType(AppTypeEnum.APP_STATUS.getCode());
        model.addAttribute("statusList",dictionaryVOList);

        //查询全部所属平台
        List<DataDictionaryVO> dataDictionaryVOS = dataDictionaryService.findValueByType(AppTypeEnum.APP_FLATFORM.getCode());
        model.addAttribute("flatformList",dataDictionaryVOS);

        //查询全部一级目录
        List<AppCategoryVO> appCategoryVOList = appCategoryService.findVOByParentId(AppConstant.LEVEL_ONE_PARENT_ID);
        model.addAttribute("levelOneList",appCategoryVOList);

        return "dev/app/appMaintain";
    }

    //查询二三级分类目录
    @GetMapping("/typeLevel")
    @ResponseBody
    public ResultVO levelTwo(Long parentId){
        if (parentId==null||parentId<=0){
            log.info("【查询分级目录】 接收参数有误！ parentId={}",parentId);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR);
        }
        //查询全部二级目录
        List<AppCategoryVO> appCategoryVOList = appCategoryService.findVOByParentId(parentId);

        return ResultVOUtil.ok(appCategoryVOList);
    }

    //分页展示数据
    @GetMapping("/table-list")
    @ResponseBody
    public TableListVO<AppMaintainVO> tableList(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer limit,
                                                AppMaintainFrom from){
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);
        TableListVO<AppMaintainVO> vo = appInfoService.findPage(map);

        return vo;
    }

    //展示添加appInfo页面
    @GetMapping("/add-appInfo-ui")
    public String addAppUI(Model model){

        //查询全部所属平台
        List<DataDictionaryVO> dataDictionaryVOS = dataDictionaryService.findValueByType(AppTypeEnum.APP_FLATFORM.getCode());
        model.addAttribute("flatformList",dataDictionaryVOS);

        //查询全部一级目录
        List<AppCategoryVO> appCategoryVOList = appCategoryService.findVOByParentId(AppConstant.LEVEL_ONE_PARENT_ID);
        model.addAttribute("levelOneList",appCategoryVOList);

        return "dev/app/addAppInfo-ui";
    }

    //执行添加
    @PostMapping("/addAppInfo")
    @ResponseBody
    public ResultVO addAppInfo(@Valid AddAppInfoFrom from, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加APP信息】 接收参数有误！msg={}",msg);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),msg);
        }

        AppInfo appInfo = new AppInfo();
        BeanUtils.copyProperties(from,appInfo);
        appInfoService.addAppInfo(appInfo);


        return ResultVOUtil.ok();
    }

    //执行上架操作
    @PostMapping("/up")
    @ResponseBody
    public ResultVO up(@RequestParam(value = "ids[]",required = false) List<Long> ids){

        if (ids==null||ids.size()==0){
            log.info("【APP上架】 接收上架ids参数有误！ids={}",ids);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),"上架商品不能为空！");
        }

        appInfoService.updateAppStatusUPByIds(ids);

        return ResultVOUtil.ok();
    }

    //执行下架操作
    @PostMapping("/down")
    @ResponseBody
    public ResultVO down(@RequestParam(value = "ids[]",required = false) List<Long> ids){

        if (ids==null||ids.size()==0){
            log.info("【APP下架】 接收下架ids参数有误！ids={}",ids);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),"下架商品不能为空！");
        }

        appInfoService.updateAppStatusDOWNByIds(ids);

        return ResultVOUtil.ok();
    }

    //执行删除操作
    @PostMapping("/delete")
    @ResponseBody
    public ResultVO delete(@RequestParam(value = "ids[]",required = false) List<Long> ids){

        if (ids==null||ids.size()==0){
            log.info("【APP删除】 接收删除的ids参数有误！ids={}",ids);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),"删除的商品不能为空！");
        }

        appInfoService.deleteByIds(ids);

        return ResultVOUtil.ok();
    }

    //跳转到新增APP版本页面
    @GetMapping("/addAppVersion-ui")
    public String addAppVersionUI(Integer id, Model model){

        model.addAttribute("appId",id);
        List<AppVersionVO> versionList = appVersionService.findAppVersion(id);
        model.addAttribute("appVersionList",versionList);

        //查询全部可选的版本状态
        List<DataDictionaryVO> list = dataDictionaryService.findValueByType(AppTypeEnum.PUBLISH_STATUS.getCode());
        model.addAttribute("publishStatusList",list);

        return "dev/app/addAppVersion-ui";
    }


    //执行添加APP版本信息
    @PostMapping("addAppVersion")
    @ResponseBody
    public ResultVO addAppVersion(@Valid AddAppVersionFrom from, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加版本信息】 接收参数不合法！from={} msg={}",from,msg);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),msg);
        }

        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(from,appVersion);
        appVersionService.addVersion(appVersion);


        return ResultVOUtil.ok(from.getVersionNo());
    }


    //跳转到编辑页面

    @GetMapping("/editApp-ui")
    public String editAppUI(Long id, Model model){

        AppInfo appInfo = appInfoService.findAppInfoById(id);

        List<AppCategory> levelOneList = appCategoryService.findByParentId(AppConstant.LEVEL_ONE_PARENT_ID);

        List<AppCategory> levelTwoList = appCategoryService.findByParentId(appInfo.getCategoryLevel1());

        List<AppCategory> levelThreeList = appCategoryService.findByParentId(appInfo.getCategoryLevel2());

        List<DataDictionaryVO> flatformList = dataDictionaryService.findValueByType(AppTypeEnum.APP_FLATFORM.getCode());

        model.addAttribute("appInfo",appInfo);

        model.addAttribute("levelThreeList",levelThreeList);
        model.addAttribute("levelTwoList",levelTwoList);
        model.addAttribute("levelOneList",levelOneList);

        model.addAttribute("flatformList",flatformList);


        return "dev/app/editAppInfo";
    }


    //执行编辑操作
    @PostMapping("/editAppInfo")
    @ResponseBody
    public ResultVO editAppInfo(@Valid AddAppInfoFrom from, BindingResult bindingResult,Long id){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【添加APP信息】 接收参数有误！msg={}",msg);
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),msg);
        }
        if (id<0||id==null){
            log.info("【添加APP信息】 接收参数有误！msg={}","id为毕传项，岂能为空！");
            return ResultVOUtil.error(AppEnum.PARAM_ERROR.getCode(),"id为毕传项，岂能为空！");
        }

        AppInfo appInfo = new AppInfo();
        BeanUtils.copyProperties(from,appInfo);
        appInfo.setId(id);
        appInfoService.updateAppInfo(appInfo);

        return ResultVOUtil.ok();
    }

    //跳转到编辑APP版本信息页面
    @GetMapping("/editAppVersion-ui")
    public String editAppVersion(Integer id, Model model) {

        model.addAttribute("appId", id);
        List<AppVersionVO> versionList = appVersionService.findAppVersion(id);
        model.addAttribute("appVersionList", versionList);

        //查询全部可选的版本状态
        List<DataDictionaryVO> list = dataDictionaryService.findValueByType(AppTypeEnum.PUBLISH_STATUS.getCode());
        model.addAttribute("publishStatusList", list);

        return "dev/app/editAppVersion-ui";
    }


















}
